package cn.iocoder.yudao.module.handguard.dal.mysql.applicationrecord;

import java.math.BigDecimal;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.handguard.controller.admin.order.vo.OrderUserRespVO;
import cn.iocoder.yudao.module.handguard.controller.app.orders.vo.OrdersResVo;
import cn.iocoder.yudao.module.handguard.controller.app.orders.vo.TJOrderResVo;
import cn.iocoder.yudao.module.handguard.dal.dataobject.applicationrecord.ApplicationRecordDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.handguard.tool.MybatisParameterUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.handguard.controller.app.applicationrecord.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 用户申请贷款表（申请通过创建订单） Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ApplicationRecordMapper extends BaseMapperX<ApplicationRecordDO> {

    default PageResult<ApplicationRecordDO> selectPage(ApplicationRecordPageReqVO reqVO) {
        LambdaQueryWrapperX<ApplicationRecordDO> queryWrapper = new LambdaQueryWrapperX<ApplicationRecordDO>()
                .eqIfPresent(ApplicationRecordDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ApplicationRecordDO::getProductId, reqVO.getProductId())
                .eqIfPresent(ApplicationRecordDO::getFlowN, reqVO.getFlowN())
                .eqIfPresent(ApplicationRecordDO::getWhether,reqVO.getWhether())
                .betweenIfPresent(ApplicationRecordDO::getIssuanceTime, reqVO.getIssuanceTime())
                .eqIfPresent(ApplicationRecordDO::getAmount, reqVO.getAmount())
                .eqIfPresent(ApplicationRecordDO::getPracticalAmount, reqVO.getPracticalAmount())
                .eqIfPresent(ApplicationRecordDO::getAgentId, reqVO.getAgentId())
                .eqIfPresent(ApplicationRecordDO::getWarrantId, reqVO.getWarrant_id())
                .eqIfPresent(ApplicationRecordDO::getApplyState, reqVO.getApplyState())
                .betweenIfPresent(ApplicationRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ApplicationRecordDO::getCreateTime);

//        if (reqVO.getIds()!=null && reqVO.getIds().size()>0){
//            try {
//                MybatisParameterUtils.cutInParameter(queryWrapper, ApplicationRecordDO::getUserId, reqVO.getIds());
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        }
        return selectPage(reqVO, queryWrapper);
    }

    default List<ApplicationRecordDO> selectList(ApplicationRecordExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ApplicationRecordDO>()
                .eqIfPresent(ApplicationRecordDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ApplicationRecordDO::getProductId, reqVO.getProductId())
                .eqIfPresent(ApplicationRecordDO::getFlowN, reqVO.getFlowN())
                .betweenIfPresent(ApplicationRecordDO::getIssuanceTime, reqVO.getIssuanceTime())
                .eqIfPresent(ApplicationRecordDO::getAmount, reqVO.getAmount())
                .eqIfPresent(ApplicationRecordDO::getPracticalAmount, reqVO.getPracticalAmount())
                .eqIfPresent(ApplicationRecordDO::getApplyState, reqVO.getApplyState())
                .betweenIfPresent(ApplicationRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ApplicationRecordDO::getCreateTime));
    }

    List<OrdersResVo> selectOrderByUserId(@Param("userId") Long userId,@Param("status")Integer status);


    //获取当前页的结果列表
     List<ApplyRecordDO> findByPage(ApplyRecordPageReqVO pageReqVO);

    Long findByPageCount(ApplyRecordPageReqVO pageReqVO);

    void updateAgentByOrderId(@Param("agentId")Long agentId,@Param("orderId") Long orderId);

    void updateWarrantByOrderId(@Param("warrantId")Long agentId,@Param("orderId") Long orderId);

    void cancelAllocationWarrant(Long orderId);

    void cancelAllocationAgent(Long orderId);

    List<TJOrderResVo> getTjClientList(Long userId);

    OrderUserRespVO getUserAppllicationInfo(@Param("orderNo")Long orderNo);

    List<ApplyRecordDO> selectListAll(ApplyRecordPageReqVO pageReqVO);
}
